package com.uenpay.im.invokeitems.contacts;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qyx.android.entity.FriendEntity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.serializations.FriendSerializer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 同意好友请求
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class AcceptFriendInvokeItem extends HttpInvokeItem {
	public class AcceptFriendResult {
		public int status;
		public String msg;
		public FriendEntity friend;
	}

	public AcceptFriendResult getOutput() {
		return (AcceptFriendResult) GetResultObject();
	}

	public AcceptFriendInvokeItem(String noticeId) {
		String pattern = "Friend/Friend/acceptFriend?"
				+ APIConfiguration.getCustIdAndToken();
		HashMap<String, String> mRequestParams = new HashMap<String, String>();
		mRequestParams.put("noticeid", noticeId);
		mRequestParams.put("accept", "1");
		SetRequestParams(mRequestParams);
		String url = APIConfiguration.getBaseUrl() + pattern;
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		AcceptFriendResult result = new AcceptFriendResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			if (result.status == HttpInvokeResultCode.RESULT_OK) {
				result.friend = FriendSerializer.DeserializeFriend(json
						.optJSONObject("data"));
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}
}
